package com.heima.article.controller.v1;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.heima.article.entity.ApArticle;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.util.BeanHelper;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.dtos.ArticleInfoDto;
import com.heima.model.media.dtos.WmNewsDto;
import com.heima.model.media.dtos.WmNewsResultDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.article.service.IApArticleService;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * <p>
 * 文章信息表，存储已发布的文章 前端控制器
 * </p>
 *
 * @author heima
 * @since 2021-03-19
 */
@RestController
@RequestMapping("/api/v1/article")
@Api(tags = "文章信息表，存储已发布的文章接口")
@CrossOrigin
public class ApArticleController{

    @Autowired
    private IApArticleService apArticleService;

    /**
     * 保存文章数据
     * @param wmNewDto
     * @return
     */
    @PostMapping("/save")
    public Long saveArticle(@RequestBody WmNewsResultDTO wmNewDto){
        return apArticleService.saveArticle(wmNewDto);
    }

    /**
     * 加载首页文章
     * @return
     */
    @PostMapping("/load")
    public ResponseResult<List<ArticleDto>> load(@RequestBody ArticleHomeDto dto){
       // List<ArticleDto> dtoList = apArticleService.load(dto,1);
        List<ArticleDto> dtoList = apArticleService.loadFirst(dto);
        return ResponseResult.ok(dtoList);
    }

    /**
     * 加载更多文章
     * @return
     */
    @PostMapping("/loadmore")
    public ResponseResult<List<ArticleDto>> loadMore(@RequestBody ArticleHomeDto dto){
        List<ArticleDto> dtoList = apArticleService.load(dto,1);
        return ResponseResult.ok(dtoList);
    }

    /**
     * 加载最新文章
     * @return
     */
    @PostMapping("/loadnew")
    public ResponseResult<List<ArticleDto>> loadNew(@RequestBody ArticleHomeDto dto){
        List<ArticleDto> dtoList = apArticleService.load(dto,2);
        return ResponseResult.ok(dtoList);
    }

    /**
     * 加载文章详情数据
     */
    @PostMapping("/load_article_info")
    public ResponseResult<Map<String,Object>> loadArticleInfo(@RequestBody ArticleInfoDto dto){
        Map<String,Object> map = apArticleService.loadArticleInfo(dto);
        return ResponseResult.ok(map);
    }

    /**
     * 加载文章详情页面行为数据
     */
    @PostMapping("/load_article_behavior")
    public ResponseResult<Map<String,Boolean>> loadArticleBehavior(@RequestBody ArticleInfoDto dto){
        Map<String,Boolean> map =apArticleService.loadArticleBehavior(dto);
        return ResponseResult.ok(map);
    }

    /**
     * 查询所有未下架、未删除的文章列表
     */
    @GetMapping("/findByPage")
    List<ArticleDto> findByPage(@RequestParam(name = "page") int page,
                                @RequestParam(name = "size") int size){
       return apArticleService.findByPage(page,size);
    }

    /**
     * 基于文章id查询文章数据
     */
    @GetMapping("/{id}")
    public ArticleDto findArticleById(@PathVariable("id") Long articleId){
        ApArticle apArticle = apArticleService.getById(articleId);
        if(apArticle!=null){
            return BeanHelper.copyProperties(apArticle,ArticleDto.class);
        }
        return null;
    }
}
